﻿<link href="@Url.Content("/content/styles/order.css")" rel="stylesheet" type="text/css" />

<div class="ld-tabs">
    <ul>
        <li class="k-state-active">填写 @Invoicing.Web.Management.Common.GetOrderName(ViewBag.Method)</li>
    </ul>
    <div>
        <div class="k-block form_title">
            <div>
                <b>客户：</b><input id="txtCustomer" data-value="0" style="width: 400px" />
                <b>日期：</b><input id="txtNoteDate" />
            </div>
            <div style="margin-top: 10px;">
                <b>备注：</b><input type="text" class="k-textbox" id="txtDescription" style="width: 90%" />
            </div>
            @if (ViewBag.Method == 31){
                <div style="margin-top: 10px;">
                    <b>调出销售订单：</b>
                    <span class="k-textbox k-space-right" style="vertical-align:middle;width:300px">
                        <input type="text" placeholder="请输入销售订单号" id="txtSellOrderNo" />
                        <a href="javascript:LoadSellBook()" class="k-icon k-i-search">&nbsp;</a>
                    </span>
                </div>
            }
        </div>
        <div id="grid_orders"></div>
    </div>
</div>

<script class="init">
    var submenu;
    @if (ViewBag.Method == 31)
    {
        <text>submenu = "新采购订单";</text>
    }
    else if (ViewBag.Method == 32)
    {
        <text>submenu = "新销售订单";</text>
    }

    LocatedMenu("预订管理", submenu);
    $("#txtNoteDate").kendoDatePicker({
        value: new Date(),
        culture: "zh-CHS",
        format: "yyyy年MM月dd日"
    });

    var allCustomer = Whale.Remote.GetEntity({
        url: "/Members/Customer/Gets",
        parameters: {
            cType: "@(ViewBag.Method % 2 == 0 ? 1 : 2)",
            customerStatus: 1
        }
    });

    $("#txtCustomer").kendoComboBox({
        placeholder: "请选择客户...",
        filter: "contains",
        dataTextField: "SearchLabel",
        template: "#=ComplexName#",
        dataValueField: "ID",
        dataSource: allCustomer,
        select: function (e) {
            var item = this.dataItem(e.item.index());
            $("#txtCustomer").attr("data-value", item.ID);
        },
        change: function (e) {
            var item = this.dataItem();
            if (item != undefined) {
                this.text(item.ComplexName);
            } else {
                var b = true;
                var obj = this;
                $.each(allCustomer, function (i, o) {
                    if (obj.text() == o.ComplexName) {
                        b = false;
                    }
                });
                if (b) {
                    this.text("");
                    $("#txtCustomer").attr("data-value", "0");
                }
            }
        }
    });
</script>

<script class="grid_orders_columns">
    var orderFields = [{
        title: "产品型号",
        field: "Product",
        template: "#=Product.Module#",
        editor: productEditor,
        width: 200
    }, {
        title: "材料",
        field: "Material",
        template: "#=Material.MaterialName#",
        editor: materialEditor,
        width: 120
    }, {
        title: "加工",
        field: "Process",
        template: "#=Process.ProcessName#",
        editor: processEditor,
        width: 100
    }, {
        title: "规格",
        field: "Size",
        template: "#=Size.SizeName#",
        editor: sizeEditor,
        width: 100
    }, {
        title: "数量(只)",
        field: "Quantity",
        editor: quantityEditor,
        width: 100
    }, {
        title: "操作",
        command: [{
            name: "destroy",
            text: "删除"
        }],
        width: 100
    }, {}];

    var allProducts = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0,
            productStatus: 1
        }
    });
    var materials = Whale.Remote.GetEntity({
        url: "/Products/Material/Gets"
    });
    var processes = Whale.Remote.GetEntity({
        url: "/Products/Process/Gets"
    });
    var sizes = Whale.Remote.GetEntity({
        url: "/Products/Size/Gets"
    });

    function productEditor(container, options) {
        var selPro = $('<input required data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoComboBox({
                autoBind: false,
                filter: "contains",
                dataTextField: "Module",
                dataValueField: "ID",
                dataSource: allProducts,
                change: function (e) {
                    if (selPro.dataItem() == null || selPro.dataItem() == undefined) {
                        $("#grid_orders").data("kendoGrid").cancelRow();
                        return;
                    }
                    var product = selPro.dataItem();
                    options.model.Product = product;

                    if (product.PType == 1) {
                        options.model.Material = { ID: 0, MaterialName: "" };
                        options.model.Process = { ID: 1, ProcessName: "无" };
                        options.model.Size = { ID: 0, SizeName: "无" };

                    } else {
                        options.model.Material = { ID: 0, MaterialName: "" };
                        options.model.Process = { ID: 0, ProcessName: "" };
                        options.model.Size = { ID: 0, SizeName: "" };
                    }

                    $("#grid_orders").data("kendoGrid").refresh();
                }
            }).data("kendoComboBox");
    }

    function materialEditor(container, options) {
        var selMaterial = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "MaterialName",
                dataValueField: "ID",
                dataSource: {
                    transport: {
                        read: function (e) {
                            var ret = $.grep(materials, function (n, i) {
                                return n.MType == options.model.Product.PType;
                            });
                            e.success(ret);
                        }
                    }
                },
                dataBound: function (e) {
                    if (options.model.Material.ID == 0) {
                        options.model.Material = this.dataItem(0);
                    }
                }
            }).data("kendoDropDownList");
    }

    function processEditor(container, options) {
        var selProcess = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "ProcessName",
                dataValueField: "ID",
                dataSource: {
                    transport: {
                        read: function (e) {
                            var ret;
                            if (options.model.Product.PType == 1) {
                                ret = $.grep(processes, function (n, i) {
                                    return n.ID == 1;
                                });
                            } else {
                                ret = $.grep(processes, function (n, i) {
                                    return n.ID > 1;
                                });
                            }
                            e.success(ret);
                        }
                    }
                },
                dataBound: function (e) {
                    if (options.model.Process.ID == 0) {
                        options.model.Process = this.dataItem(0);
                    }
                }
            }).data("kendoDropDownList");
    }

    function sizeEditor(container, options) {
        var selSize = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "SizeName",
                dataValueField: "SizeName",
                dataSource: {
                    transport: {
                        read: function (e) {
                            var ret = $.grep(sizes, function (n, i) {
                                return n.PType == options.model.Product.PType;
                            });
                            e.success(ret);
                        }
                    }
                },
                dataBound: function (e) {
                    if (options.model.Size.SizeName == "")
                        options.model.Size = this.dataItem(0);
                    else {

                    }
                }
            }).data("kendoDropDownList");
    }

    function quantityEditor(container, options) {
        var txtQuantity = $('<input data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoNumericTextBox({
                decimals: 3,
                change: function (e) {
                    //RefreshRow(options.model);
                }
            }).data("kendoNumericTextBox");
    }

</script>

<script class="grid_orders_dataSource">
    var orderDataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    Product: { defaultValue: { ID: 0, ProductName: "", UnitName: "" } },
                    Material: { defaultValue: { ID: 0, MaterialName: "" } },
                    Process: { defaultValue: { ID: 0, ProcessName: "" } },
                    Size: { defaultValue: { SizeName: "" } },
                    Quantity: {},
                }
            }
        }
    });
</script>

<script class="grid_orders_config">
    var hasSaved = false;
    $("#grid_orders").kendoGrid({
        columns: orderFields,
        pageable: false,
        toolbar: [{
            name: "create",
            text: "新增一行"
        }, {
            name: "save",
            text: "保存单据"
        }],
        editable: true,
        dataBound: function (e) {
            //让表头居中
            $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            $("a.k-grid-save-changes").attr("style", "font-weight:bold; float:right");
        },
        dataSource: orderDataSource,
        saveChanges: function (e) {
            if (hasSaved) return; else hasSaved = true;
            var values = {
                orderDate: kendo.toString($("#txtNoteDate").data("kendoDatePicker").value(), "yyyy-MM-dd"),
                customerID: $("#txtCustomer").attr("data-value"),
                method: "@ViewBag.Method",
                description: $("#txtDescription").val(),
                orderItems: [],
                orderNo:"@ViewBag.OrderNo"
            };
            values.orderItems = $.map(orderDataSource.data(), function (o) {
                return {
                    productID: o.Product.ID,
                    materialID: o.Material.ID,
                    processID: o.Process.ID,
                    size: o.Size.SizeName,
                    quantity: o.Quantity,
                }
            });
            Whale.Remote.Operation({
                url: "/Orders/BookOrder/SaveOrders",
                parameters: values,
                success: function (result) {
                    location.href = "/Success/PrintBookOrder/" + result.OrderNo;
                },
                error: function (msg) {
                    hasSaved = false;
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    });
</script>

<script class="functions">
    var orderNo = "@ViewBag.OrderNo";
    $(function () {
        if (orderNo != "") {
            var existOrder = Whale.Remote.GetEntity({
                url: "/Orders/BookOrder/GetOrder",
                parameters: { orderNo: orderNo }
            });
            if (existOrder != null) {
                $("#txtCustomer").attr("data-value", existOrder.CustomerID);
                $("#txtCustomer").data("kendoComboBox").text(existOrder.Customer.ComplexName);
                $("#txtNoteDate").data("kendoDatePicker").value(kendo.parseDate(existOrder.NoteDate));
                $.each(existOrder.Items, function (i, o) {
                    o.Size = { SizeName: o.Size };
                });
                orderDataSource.data(existOrder.Items);
                $("#grid_orders").data("kendoGrid").setDataSource(orderDataSource);
            }
        }
    });
    
</script>

<script class="LoadSellBook">
    function LoadSellBook() {
        var sellOrderNo = $("#txtSellOrderNo").val();
        if (sellOrderNo != "") {
            var sellOrder = Whale.Remote.GetEntity({
                url: "/Orders/BookOrder/GetOrder",
                parameters: { orderNo: sellOrderNo }
            });
            if (sellOrder != null) {
                $.each(sellOrder.Items, function (i, o) {
                    o.Size = { SizeName: o.Size };
                });
                orderDataSource.data(sellOrder.Items);
                $("#grid_orders").data("kendoGrid").setDataSource(orderDataSource);
            }
        }
    }
</script>
 